package cn.kgm.bio.threadLink;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo {
    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(6666);
        while (true){
            // 在这里等待客户端链接，链接成功时会创建一个socket用于通信
            Socket socket = server.accept();
            new ServerReadThread(socket).start();
        }
    }

   private static class ServerReadThread extends Thread{
        Socket socket;

        ServerReadThread(Socket socket){
            this.socket = socket;
        }

       @Override
       public void run() {
            try{
                InputStream is = this.socket.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String line;
                while ((line = br.readLine()) != null){
                    System.out.println("服务端阅读到的消息: "+ line);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
       }
   }
}
